/**
 * Created by Elsa on 2017/9/12.
 */
layui.define(['jquery', 'layer','paging', 'code','form','upload'], function(exports){
    var form = layui.form();
    var index;

    layui.code();
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        upload = layui.upload;

    var name = decodeURI(getQueryString("name"));
    var id = getQueryString("id");
    var parentid = getQueryString("parentid");
    console.log(name+" "+parentid);
    $('#legend').html("# "+name);
    var therole = "";
    $.ajax({
        type:"GET",
        url:contextPath+"/personal/role",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            therole = data.role;
            if(data.role=="管理员"){
                $('#add').show();
                $('#newnode').show();
                $('#deletenode').show();
                $('#renamenode').show();
            }else if(data.role=="负责人"){
                $('#add').show();
                $('#newnode').show();
                $('#deletenode').show();
                $('#renamenode').show();
            }else{

            }
        },
        error: function (e) {

        }
    });

    paging.init({
        url: contextPath + "/statute/"+ id, //地址
        elem: '#con', //内容容器
        type: 'GET',
        params: { //发送到服务端的参数

        },
        tempElem: '#conTemp', //模块容器
        pageConfig: { //分页参数配置
            elem: '#page', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
            if(therole=="管理员"||therole=="负责人"){
                $('.delete').show(); 
            }
        },
        fail: function(msg) { //获取数据失败的回调
        },
        complate: function() { //完成的回调
        },
    });

    $("body").on("click",".delete",function(){
        var statuteid = $(this).data('id');

        layer.open({
            type: 1
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: '300px;'
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,resize: false
            ,btn: ['确认删除', '取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: '<div style="height: 60px;text-align: center;padding-top: 40px; background-color: #393D49; color: #fff; font-weight: 300;">是否删除该法规？</div>'
            ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.find('.layui-layer-btn0').on('click', function () {
                    $.ajax({
                        type:"GET",
                        url:contextPath+"/statute/delete/"+statuteid,
                        contentType: "application/json; charset=utf-8",
                        dataType:"json",
                        success:function (data) {
                            console.log(data.message);
                            if(data.message == "删除成功!"){
                                layer.msg('删除成功!<br>页面将在1秒后刷新...');
                                setTimeout(function(){window.location.reload();},1000);
                            } else {
                               layer.msg(data.message, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                            }
                        },
                        error:function (e) {
                        }
                    });
                });
            }
        });

    });

    $("body").on("click",".download",function(){
        var statuteurl = $(this).data('id');

        $(this).attr("href",contextPath+"/download?file="+statuteurl);
    });


    $('#newnode').on('click', function() {
        var con = '<div style="padding:10px;"><input type="text" class="layui-input" style="margin-bottom:5px;" name="name" placeholder="节点名称" /></div><div align = "center"><p style="color:red">节点名称不超过30个字</p></div>';

        layer.open({
            type: 1,
            title: '新增节点',
            content: con,
            btn: ['确定', '取消'],
            area: ['350px', '200px'],
            yes: function (index, layero) {
                //这是核心的代码。
                var NewNode = new Object();
                NewNode.id = randomChar(3);
                NewNode.pid = id;
                NewNode.name = $(layero).find('input[name="name"]').val();

                console.log(JSON.stringify(NewNode));

                $.ajax({
                    type:"PUT",
                    url:contextPath+"/statute/new",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify(NewNode),
                    dataType:"json",
                    success:function (data) {
                        console.log(data);
                        if(data == "新增成功!"){
                            layer.msg('新增成功!<br>页面将在1秒后刷新...');
                            setTimeout(function(){parent.window.location.reload();},1000);
                        } else {
                            layer.msg(data, {
                                time:false,
                                btn:"确定",
                                btnAlign: 'c'
                            });
                        }
                    },
                    error:function (e) {
                    }
                });


            },
            maxmin: false
        });
    });

    $('#renamenode').on('click', function() {
        var con = '<div style="padding:10px;"><input type="text" class="layui-input" style="margin-bottom:5px;" name="name" placeholder="节点名称" /></div>';

        layer.open({
            type: 1,
            title: '修改节点',
            content: con,
            btn: ['确定', '取消'],
            area: ['350px', '170px'],
            yes: function (index, layero) {
                //这是核心的代码。
                var RenameNode = new Object();
                RenameNode.id = id;
                RenameNode.name = $(layero).find('input[name="name"]').val();

                console.log(JSON.stringify(RenameNode));

                $.ajax({
                    type:"POST",
                    url:contextPath+"/statute/update",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify(RenameNode),
                    dataType:"json",
                    success:function (data) {
                        console.log(data);
                        if(data == "更新成功!"){
                            layer.msg('更新成功!<br>页面将在1秒后刷新...');
                            setTimeout(function(){parent.window.location.reload();},1000);
                        } else {
                            layer.msg(data, {
                                time:false,
                                btn:"确定",
                                btnAlign: 'c'
                            });
                        }
                    },
                    error:function (e) {
                    }
                });


            },
            maxmin: false
        });
    });

    $('#deletenode').on('click', function() {

        layer.open({
            type: 1
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: '300px;'
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,resize: false
            ,btn: ['确认删除', '取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: '<div style="height: 60px;text-align: center;padding-top: 40px; background-color: #393D49; color: #fff; font-weight: 300;">是否删除该节点？</div>'
            ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.find('.layui-layer-btn0').on('click', function () {
                    $.ajax({
                        type:"DELETE",
                        url:contextPath+"/statute/delete/"+id,
                        contentType: "application/json; charset=utf-8",
                        dataType:"json",
                        success:function (data) {
                            console.log(data);
                            if(data == "删除成功!"){
                                layer.msg('删除成功!<br>页面将在1秒后刷新...');
                                setTimeout(function(){parent.window.location.reload();},1000);
                            } else {
                                layer.msg(data, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                            }
                        },
                        error:function (e) {
                        }
                    });
                });
            }
        });
    });

    $('#add').on('click', function() {
        var con = '<div style="padding:10px;" ><label class="layui-form-label" style="width: 60px">上传者</label><input type="text" class="layui-input" style="margin-bottom:5px;width:150px" name="name"/>'+
                    '<button type="button" class="layui-btn" id="upload"><i class="fa fa-upload"></i>  上传文件</button></div><div><label style="margin-left:20px">文件大小不超过20MB，文件名长度不超过40，</label></div><div><label style="margin-left:20px">只能上传pdf、word文件</label></div>';

        layer.open({
            type: 1,
            title: '新增法规',
            content: con,
            btn: ['确定', '取消'],
            area: ['320px', '250px'],
            success: function (index, layero) {
                //这是核心的代码。
                $('input[name="name"]').attr('disabled',true);
                $('input[name="name"]').attr('class',"layui-input layui-disabled");
                $.ajax({
                    type: "GET",
                    url: contextPath + "/personal/info",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $('input[name="name"]').attr("value",data.name);
                        var uploader = data.name;
                        upload.render({
                            elem: '#upload'
                            ,url: contextPath + '/statute/new?statuteid='+randomChar(4)+'&parentid='+id+'&uploader='+uploader
                            ,size: 2048
                            ,accept: 'file' //普通文件
                            , exts: 'docx|doc|pdf|html|xml|txt'
                            ,auto: false
                            ,bindAction: '.layui-layer-btn0'
                            ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                                layer.load(); //上传loading
                            }
                            , done: function (res, index, upload) { //上传后的回调
                                layer.closeAll('loading');
                                console.log(res);
                                if(res.msg == '上传成功'){
                                    layer.msg('上传成功!<br>页面将在1秒后刷新...');
                                    setTimeout(function(){window.location.reload();},1000);
                                }
                            }
                            ,error: function(index, upload){
                                layer.closeAll('loading'); //关闭loading
                            }
                        });

                    },
                    error: function (e) {
                    }
                });
            },
            maxmin: false
        });
    });



    exports('RegList', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});